package com.eshop.modules.user.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.common.service.impl.*;
import com.eshop.modules.user.service.mapper.*;
import com.eshop.modules.user.domain.*;
import com.eshop.modules.user.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;
import com.eshop.dozer.service.*;
import java.math.*;
import com.baomidou.mybatisplus.core.conditions.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.eshop.domain.*;
import java.util.*;
import com.eshop.modules.user.vo.*;
import com.eshop.enums.*;
import com.baomidou.mybatisplus.core.metadata.*;
import org.springframework.data.domain.*;
import com.github.pagehelper.*;
import com.eshop.modules.user.service.dto.*;
import javax.servlet.http.*;
import com.eshop.utils.*;
import java.io.*;
import java.lang.invoke.*;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class UserBillServiceImpl extends BaseServiceImpl<UserBillMapper, UserBill> implements UserBillService
{
    private final IGenerator generator;
    private final UserBillMapper yxUserBillMapper;
    
    public void expend(final Long uid, final String title, final String category, final String type, final double number, final double balance, final String mark) {
        final UserBill userBill = UserBill.builder().uid(uid).title(title).category(category).type(type).number(BigDecimal.valueOf(number)).balance(BigDecimal.valueOf(balance)).mark(mark).pm(BillEnum.PM_0.getValue()).build();
        this.yxUserBillMapper.insert(userBill);
    }
    
    public void income(final Long uid, final String title, final String category, final String type, final double number, final double balance, final String mark, final String linkid) {
        final UserBill userBill = UserBill.builder().uid(uid).title(title).category(category).type(type).number(BigDecimal.valueOf(number)).balance(BigDecimal.valueOf(balance)).mark(mark).pm(BillEnum.PM_1.getValue()).linkId(linkid).build();
        this.yxUserBillMapper.insert(userBill);
    }
    
    public int cumulativeAttendance(final Long uid) {
        final LambdaQueryWrapper<UserBill> wrapper = (LambdaQueryWrapper<UserBill>)new LambdaQueryWrapper();
        (((wrapper.eq(UserBill::getUid, uid)).eq(UserBill::getCategory, "integral")).eq(UserBill::getType, "sign")).eq(UserBill::getPm, 1);
        return this.yxUserBillMapper.selectCount(wrapper);
    }
    
    public Map<String, Object> spreadOrder(final Long uid, final int page, final int limit) {
        final QueryWrapper<UserBill> wrapper = (QueryWrapper<UserBill>)new QueryWrapper();
        ((wrapper.lambda().in(UserBill::getUid, new Object[] { uid })).eq(UserBill::getType, BillDetailEnum.TYPE_2.getValue())).eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
        ((QueryWrapper)wrapper.orderByDesc("time")).groupBy("time");
        final Page<UserBill> pageModel = (Page<UserBill>)new Page((long)page, (long)limit);
        final List<String> list = this.yxUserBillMapper.getBillOrderList((Wrapper<UserBill>)wrapper, pageModel);

        final int count = this.yxUserBillMapper.selectCount(((Wrappers.<UserBill>lambdaQuery().eq(UserBill::getUid, uid)).eq(UserBill::getType,
                BillDetailEnum.TYPE_2.getValue())).eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue()));


        final List<BillOrderDto> listT = new ArrayList<BillOrderDto>();
        for (final String str : list) {
            final BillOrderDto billOrderDTO = new BillOrderDto();
            final List<BillOrderRecordDto> orderRecordDTOS = this.yxUserBillMapper.getBillOrderRList(str, uid);
            billOrderDTO.setChild(orderRecordDTOS);
            billOrderDTO.setCount(orderRecordDTOS.size());
            billOrderDTO.setTime(str);
            listT.add(billOrderDTO);
        }
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("list", listT);
        map.put("count", count);
        return map;
    }
    
    public List<BillVo> getUserBillList(final int page, final int limit, final long uid, final int type) {
        final QueryWrapper<UserBill> wrapper = new QueryWrapper();
        ((wrapper.lambda().eq(UserBill::getUid, uid)).orderByDesc(BaseDomain::getCreateTime)).orderByAsc(UserBill::getId);
        wrapper.groupBy("time");
        switch (BillInfoEnum.toType(type)) {
            case PAY_PRODUCT: {
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
                wrapper.lambda().eq(UserBill::getType, BillDetailEnum.TYPE_3.getValue());
                break;
            }
            case RECHAREGE: {
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
                wrapper.lambda().eq(UserBill::getType, BillDetailEnum.TYPE_1.getValue());
                break;
            }
            case BROKERAGE: {
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
                wrapper.lambda().eq(UserBill::getType, BillDetailEnum.TYPE_2.getValue());
                break;
            }
            case EXTRACT: {
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
                wrapper.lambda().eq(UserBill::getType, BillDetailEnum.TYPE_4.getValue());
                break;
            }
            case SIGN_INTEGRAL: {
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_2.getValue());
                wrapper.lambda().eq(UserBill::getType, BillDetailEnum.TYPE_10.getValue());
                break;
            }
            default: {
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
                break;
            }
        }
        final Page<UserBill> pageModel = (Page<UserBill>)new Page((long)page, (long)limit);
        final List<BillVo> billDTOList = this.yxUserBillMapper.getBillList(wrapper, pageModel);
        for (final BillVo billDTO : billDTOList) {
            final LambdaQueryWrapper<UserBill> wrapperT = new LambdaQueryWrapper();
            wrapperT.in(UserBill::getId, Arrays.asList(billDTO.getIds().split(",")));
            wrapperT.orderByDesc(BaseDomain::getCreateTime);
            billDTO.setList(this.yxUserBillMapper.getUserBillList(wrapperT));
        }
        return billDTOList;
    }
    
    public double getBrokerage(final int uid) {
        return this.yxUserBillMapper.sumPrice(uid);
    }
    
    public double yesterdayCommissionSum(final Long uid) {
        return this.yxUserBillMapper.sumYesterdayPrice(uid);
    }
    
    public List<UserBillQueryVo> userBillList(final Long uid, final String category, final int page, final int limit) {
        final LambdaQueryWrapper<UserBill> wrapper = (LambdaQueryWrapper<UserBill>)new LambdaQueryWrapper();
        (((wrapper.eq(UserBill::getStatus, ShopCommonEnum.IS_STATUS_1.getValue())).eq(UserBill::getUid, uid)).eq(UserBill::getCategory, category)).orderByDesc(UserBill::getId);
        final Page<UserBill> pageModel = (Page<UserBill>)new Page((long)page, (long)limit);
        final IPage<UserBill> pageList = (IPage<UserBill>)this.yxUserBillMapper.selectPage((IPage)pageModel, wrapper);
        return this.generator.convert(pageList.getRecords(), UserBillQueryVo.class);
    }
    
    public Map<String, Object> queryAll(final UserBillQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<YxUserBillDto> page = (PageInfo<YxUserBillDto>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<YxUserBillDto> queryAll(final UserBillQueryCriteria criteria) {
        String date = null;
        String date2 = null;
        if (StringUtils.isNotEmpty(criteria.getStartTime())) {
            date = criteria.getStartTime() + " 00:00:00";
            if (StringUtils.isNotEmpty(criteria.getEndTime())) {
                date2 = criteria.getEndTime() + " 23:59:59";
            }
        }
        return (this.baseMapper).findAllByQueryCriteria(criteria.getCategory(), criteria.getType(), criteria.getNickname(), criteria.getPm(), date, date2, criteria.getTitle());
    }

    public void download(List<YxUserBillDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList();
        Iterator var4 = all.iterator();

        while(var4.hasNext()) {
            YxUserBillDto yxUserBill = (YxUserBillDto)var4.next();
            Map<String, Object> map = new LinkedHashMap();
            map.put("用户uid", yxUserBill.getUid());
            map.put("关联id", yxUserBill.getLinkId());
            map.put("0 = 支出 1 = 获得", yxUserBill.getPm());
            map.put("账单标题", yxUserBill.getTitle());
            map.put("明细种类", yxUserBill.getCategory());
            map.put("明细类型", yxUserBill.getType());
            map.put("明细数字", yxUserBill.getNumber());
            map.put("剩余", yxUserBill.getBalance());
            map.put("备注", yxUserBill.getMark());
            map.put("0 = 带确定 1 = 有效 -1 = 无效", yxUserBill.getStatus());
            list.add(map);
        }

        FileUtil.downloadExcel(list, response);
    }
    
    public UserBillServiceImpl(final IGenerator generator, final UserBillMapper yxUserBillMapper) {
        this.generator = generator;
        this.yxUserBillMapper = yxUserBillMapper;
    }
}
